package com.iwop.moto.service.impl;

import com.iwop.moto.comm.Page;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.BaseService;
import com.iwop.moto.service.StoreService;
import com.iwop.moto.utils.stringUtil.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Created by huyuangui on 2016/7/7.
 */
@Service("storeService")
public class StoreServiceImpl extends BaseService implements StoreService {

    public static  String baseMapper="MStoreMapper.";

    @Override
    public SuperRecordModel findById(SuperRecordModel superRecordModel) {
        return getSqlSession().selectOne(baseMapper + selectByPrimaryKey, superRecordModel);
    }

    @Override
    public SuperRecordModel save(SuperRecordModel superRecordModel) {
        if(superRecordModel==null){
            return null;
        }
        if(!"".equals(superRecordModel.getString("sid")) ){
            SuperRecordModel fd =findStoreByUserId(superRecordModel);
            if(fd==null){
              getSqlSession().insert(baseMapper + insert, superRecordModel);
            }else{
                superRecordModel.put("sid",fd.get("sid"));
                getSqlSession().update(baseMapper+updateByPrimaryKey,superRecordModel);
            }
            return findById(superRecordModel);
        }
        return null;
    }

    @Override
    public Page selectByLatLog(SuperRecordModel superRecordModel, Page page) {
        if(StringUtils.isEmpty(superRecordModel.getString("log"))
                ||StringUtils.isEmpty(superRecordModel.getString("lat"))||
                StringUtils.isEmpty(superRecordModel.getString("status"))){
            return  page;
        }
        superRecordModel.put("start", page.getStartIndex());
        superRecordModel.put("end",page.getEndIndex());
        int count=getSqlSession().selectOne(baseMapper+"selectByLatLogTotal",superRecordModel);
        if(count>0){
            List<SuperRecordModel> list=getSqlSession().selectList(baseMapper+"selectByLatLog",superRecordModel);
            page.setList(list);
        }
        return page;
    }

    /**
     * @功能: 根据用户信息查询门店信息
     */
    @Override
    public SuperRecordModel findStoreByUserId(SuperRecordModel SRModel){
        return getSqlSession().selectOne(baseMapper+"findStoreByUserId",SRModel);
    }

    /**
     * @功能: 更新审核状态
     */
    @Override
    public void updateStoreStatus(SuperRecordModel SRModel){
         getSqlSession().selectOne(baseMapper+"updateStoreStatus",SRModel);
    }

    /**
     * 更新
     * @param SRModel
     */
    @Override
    public void updateByUserId(SuperRecordModel SRModel){
        getSqlSession().update("MStoreMapper.updateByUserId",SRModel);
    }


    public Long findUserAndStoreKeyCount(SuperRecordModel SRModel){
        return getSqlSession().selectOne("MStoreMapper.findUserAndStoreKeyCount",SRModel);
    }

    @Override
    public void updateStoreByNoPass(SuperRecordModel SRModel) {
        getSqlSession().update("MStoreMapper.updateStoreByNoPass",SRModel);
    }

    @Override
    public void updateStoreClick(SuperRecordModel SRModel) {
        getSqlSession().update("MStoreMapper.updateStoreClick",SRModel);
    }

    @Override
    public SuperRecordModel getStoreNameByTel(SuperRecordModel srModel) {
        return getSqlSession().selectOne("MStoreMapper.getStoreNameByTel",srModel);
    }

    @Override
    public SuperRecordModel getStoreBySid(String sid){
        return getSqlSession().selectOne("MStoreMapper.getStoreBySid",sid);
    }

    @Override
    public SuperRecordModel findUserCard(String cardno) {
        return getSqlSession().selectOne("MStoreMapper.findUserCard",cardno);
    }

    @Override
    public SuperRecordModel getStoreUserBySid(SuperRecordModel superRecordModel) {
        return getSqlSession().selectOne("MStoreMapper.getStoreUserBySid",superRecordModel);
    }

    @Override
    public void deleteMStore(String userId) {
        getSqlSession().selectOne("MStoreMapper.deleteMStore",userId);
    }

    @Override
    public List<SuperRecordModel> findStoreByStoreName(SuperRecordModel srModel) {
        return  getSqlSession().selectList("MStoreMapper.findStoreByStoreName",srModel);
    }
}